package com.dlc.shop.user.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.user.common.dao.UserLevelMapper;
import com.dlc.shop.user.common.dao.UserLevelRightsMapper;
import com.dlc.shop.user.common.enums.UserRightsInfo;
import com.dlc.shop.user.common.model.UserLevel;
import com.dlc.shop.user.common.model.UserLevelRights;
import com.dlc.shop.user.common.service.UserLevelRightsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 会员等级
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserLevelRightsServiceImpl extends ServiceImpl<UserLevelRightsMapper, UserLevelRights> implements UserLevelRightsService {

    private UserLevelRightsMapper userLevelRightsMapper;
    private UserLevelMapper userLevelMapper;
    private UserLevelRightsMapper userLevelCouponMapper;

    @Override
    public void insertBatchRights(Long[] rightsList, Long id) {
        userLevelRightsMapper.insertBatchRights(rightsList,id);
    }

    @Override
    public void delBatchRights(Long[] rightsList, Long id) {
        userLevelRightsMapper.delBatchRights(rightsList,id);
    }

    @Override
    public void removeCouponRightsByLevelIds(List<Long> levelIds) {
        // 查询会员等级对应的shopId
        List<UserLevel> userLevels = userLevelMapper.selectList(new LambdaQueryWrapper<UserLevel>().in(UserLevel::getId, levelIds));
        Set<Long> shopIds = userLevels.stream().map(UserLevel::getShopId).collect(Collectors.toSet());
        // 查询商铺对应的"送优惠券"权益
        List<Long> rightIds = userLevelRightsMapper.listCouponRightsByShopIds(shopIds);
        rightIds.add(UserRightsInfo.COUPON.value());
        // 删除会员等级对应的"送优惠券"权益
        this.remove(new LambdaQueryWrapper<UserLevelRights>()
                .in(UserLevelRights::getLevelId, levelIds)
                .in(UserLevelRights::getRightsId, rightIds)
        );
    }
}
